package com.atguigu.day03;

import com.atguigu.bean.Event;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Flink07_Transform_Filter {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将读进来的字符串转为Event
        SingleOutputStreamOperator<Event> map = streamSource.map(new MapFunction<String, Event>() {
            @Override
            public Event map(String value) throws Exception {
                System.out.println("---------------------map---------------------");
                String[] split = value.split(",");
                return new Event(split[0], split[1], Long.parseLong(split[2]));
            }
        });

        //TODO 利用filter将user为songsong的数据过滤出来
        SingleOutputStreamOperator<Event> filter = map.filter(new FilterFunction<Event>() {
            @Override
            public boolean filter(Event value) throws Exception {
                System.out.println("--------------------filter---------------------");
                return "songsong".equals(value.user);
            }
        });

        filter.print();

        env.execute();


    }
}
